package cn.sz.hy.handler.top;

import java.util.List;

import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Text;

import cn.sz.hy.common.Globel;
import cn.sz.hy.event.top.FileOpenEvent;
import cn.sz.hy.handler.Handler;
import cn.sz.hy.logic.strategy.StrategyFactory;
import cn.sz.hy.logic.strategy.impl.XlsStrategy;
import cn.sz.hy.logic.strategy.vo.SheetItem;
import cn.sz.hy.logic.strategy.vo.TableVo;
import cn.sz.hy.ui.frame.CenterPanel;

public class FileOpenHandler implements Handler<FileOpenEvent> {
	public FileOpenHandler() {
		super();
	}

	@Override
	public void execute(FileOpenEvent event) {
		FileDialog fileDialog = (FileDialog)eBus.getComponent(Globel.FILEDIALOD_ID);
		Text text = (Text) eBus.getComponent(event.getKey().hashCode());
		String path = fileDialog.open();
		
		if(path == null){
			return;
		}
		
		text.setText(path);
		if (Globel.DET != event.getTableType()) {
			Combo executeList = (Combo) eBus.getComponent(Globel.EXECUTE_LIST_ID);
			if(executeList.getSelectionIndex() == -1){
				executeList.select(0);
			}
			XlsStrategy xs = StrategyFactory
					.getStrategy(executeList.getSelectionIndex());
			List<TableVo> tempList = xs.loadHeader(path);
			if (tempList != null) {
				TableVo vo = tempList.get(0);
				eBus.addTableVo(event.getTableType(), vo);
				showHeaderList(event.getTableType(), vo, xs);
			}
		}

	}

	protected void showHeaderList(int tableType, TableVo vo, XlsStrategy xs) {
		CenterPanel centPanel = (CenterPanel) eBus
				.getComponent(Globel.CENTER_PANEL_ID);
		List<SheetItem> items = vo.getColumns();
		if (tableType == Globel.MAIN) {
			centPanel.loadItem(items);
			TableVo back = eBus.getTableVo(Globel.BACKUP);
			if (back != null) {
				centPanel.uploadBackupItemList(back.getColumns());
			}
		} else {
			centPanel.uploadBackupItemList(vo.getColumns());
		}
	}

}
